package xcmg.device.web.company;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.company.DealerClassService;
import xcmg.device.service.vo.company.DealerClassVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.Ref;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sqq on 2017/12/21.
 */
@RestController
@RequestMapping("/dealerClass")
public class DealerClassController extends BasicController {

    @Autowired
    private DealerClassService dealerClassService;

    @RequestMapping(value = "/inser",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Client.CLASS_ADD)
    public ActionResult insert(@RequestBody DealerClassVO dealerClassVO)throws Exception{
        dealerClassVO.setCreateId(getUserId());
        dealerClassVO.setUpdateId(getUserId());
        dealerClassVO.setOrgId(getOrgId());
        ErrorCode code=dealerClassService.insert(dealerClassVO);
        return actionResult(code,dealerClassVO.getId());
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Client.CLASS_EDIT)
    public ActionResult update(@RequestBody DealerClassVO dealerClassVO){
        if(dealerClassVO.getId()==null||dealerClassVO.getId()<=0){
            return actionResult(ErrorCode.IllegalArument);
        }
        dealerClassVO.setUpdateId(getUserId());
        ErrorCode code=dealerClassService.update(dealerClassVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Client.CLASS_DEL)
    public ActionResult delete(@RequestBody DealerClassVO dealerClassVO){
        if(dealerClassVO.getId()==null||dealerClassVO.getId()<=0){
            return actionResult(ErrorCode.IllegalArument);
        }
        dealerClassVO.setUpdateId(getUserId());
        Ref<List<String>> classNameList = new Ref<>(new ArrayList<>());
        ErrorCode code=dealerClassService.delete(dealerClassVO,classNameList);

        if(ErrorCode.ClassIsUsed==code)
        {
            return actionResult(code,classNameList.get());
        }
        return actionResult(code);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody DealerClassVO dealerClassVO)throws Exception{
        dealerClassVO.setOrgId(getOrgId());
        List<DealerClassVO> result=dealerClassService.list(dealerClassVO);
        return actionValueResult(result);
    }

}
